#!/usr/bin/env python
from Engine.NonPhysics_object import NonPhysics_object

class Ground(NonPhysics_object):
    def __init__(self, level, il, x, y, sc_pack):
        image = il.ground[sc_pack]
        self.sc_pack = sc_pack
        self.init(level, x, y, image[0], None)
    
    def find_coll(self, rect, level):
        self.rect = rect
        self.region = self.find_collision_region(level)
        if len(self.region) <= 0:
            return 0
        if len(level.find_collision(self.rect, self.region[0], self, False, True)) > 0:
            return 1
        else:
            return 0
    def find_coll_obj(self, rect, level):
        self.rect = rect
        self.region = self.find_collision_region(level)
        if len(self.region) <= 0:
            return None
        objs = level.find_collision(self.rect, self.region[0], self, False, True)
        if len(objs) > 0:
            return objs[0]
        else:
            return None
        
    
    def set_image_index(self, image_index, il):
        self.image_index = image_index
        self.image = il.ground[self.sc_pack][image_index]
        if [0,3,5,9,17].__contains__(self.image_index):
            self.ai_end_left = True
        else:
            self.ai_end_left = False
        if [0,4,6,10,18].__contains__(self.image_index):
            self.ai_end_right = True
        else:
            self.ai_end_right = False
    
    def find_image_index(self, level):
        rect_bak = self.rect
        self.rect = (rect_bak[0] - 4, rect_bak[1] - 4,
                     rect_bak[2] + 8, rect_bak[3] + 8)
       
        b_lu = (self.rect[0], self.rect[1], 2, 2)
        b_l = (self.rect[0], self.rect[1] + self.rect[3]/2 - 1, 2, 2)
        b_ld = (self.rect[0], self.rect[1] + self.rect[3] - 2, 2, 2)
        b_d = (self.rect[0] + self.rect[2]/2 - 1, self.rect[1] + self.rect[3] - 2, 2, 2)
        b_rd = (self.rect[0] + self.rect[2] - 2, self.rect[1] + self.rect[3] - 2, 2, 2)
        b_r = (self.rect[0] + self.rect[2] - 2, self.rect[1] + self.rect[3]/2 - 1, 2, 2)
        b_ru = (self.rect[0] + self.rect[2] - 2, self.rect[1], 2, 2)
        b_u = (self.rect[0] + self.rect[2]/2 - 1, self.rect[1], 2, 2)
        
        lu = self.find_coll(b_lu, level)
        l = self.find_coll(b_l, level)
        ld = self.find_coll(b_ld, level)
        d = self.find_coll(b_d, level)
        rd = self.find_coll(b_rd, level)
        r = self.find_coll(b_r, level)
        ru = self.find_coll(b_ru, level)
        u = self.find_coll(b_u, level)      
                
        self.rect = rect_bak
        self.region = self.find_collision_region(level)
        if (not l and not r and not u and not d):
            return 0
        elif (l and r and u and d and
              lu and ld and rd and ru):
            return 1
        elif (l and r and u and d and
              not lu and not ld and not rd and not ru):
            return 2
        elif (not l and r and not u and not d):
            return 3
        elif (l and not r and not u and not d):
            return 4
        elif (not l and not r and not u and d):
            return 5
        elif (not l and not r and u and not d):
            return 6
        elif (l and r and not u and not d):
            return 7
        elif (not l and not r and u and d):
            return 8
        elif (not l and r and not u and d and
              not rd):
            return 9
        elif (l and not r and not u and d and
              not ld):
            return 10
        elif (not l and r and u and not d and
              not ru):
            return 11
        elif (l and not r and u and not d and
              not lu):
            return 12
        elif (l and r and not u and d and
              ld and rd):
            return 13
        elif (l and r and u and not d and
              lu and ru):
            return 14
        elif (not l and r and u and d and
              rd and ru):
            return 15
        elif (l and not r and u and d and
              lu and ld):
            return 16
        elif (not l and r and not u and d
              and rd):
            return 17
        elif (l and not r and not u and d
              and ld):
            return 18
        elif (not l and r and u and not d
              and ru):
            return 19
        elif (l and not r and u and not d
              and lu):
            return 20
        elif (l and r and not u and d and
              not ld and not rd):
            return 21
        elif (l and r and u and not d and
              not lu and not ru):
            return 22
        elif (not l and r and u and d and
              not rd and not ru):
            return 23
        elif (l and not r and u and d and
              not lu and not ld):
            return 24
        elif (l and r and u and d and
              lu and not ld and not rd and ru):
            return 25
        elif (l and r and u and d and
              not lu and ld and rd and not ru):
            return 26
        elif (l and r and u and d and
              lu and ld and not rd and not ru):
            return 27
        elif (l and r and u and d and
              not lu and not ld and rd and ru):
            return 28
        elif (l and r and u and d and
              not lu and ld and rd and ru):
            return 29
        elif (l and r and u and d and
              lu and ld and rd and not ru):
            return 30
        elif (l and r and u and d and
              lu and not ld and rd and ru):
            return 31
        elif (l and r and u and d and
              lu and ld and not rd and ru):
            return 32
        elif (l and r and not u and d
              and not ld and rd):
            return 33
        elif (l and r and not u and d
              and ld and not rd):
            return 34
        elif (l and r and u and not d and
              not lu and ru):
            return 35
        elif (l and r and u and not d and
              lu and not ru):
            return 36
        elif (l and not r and u and d and
              not lu and ld):
            return 37
        elif (not l and r and u and d and
              rd and not ru):
            return 38
        elif (l and not r and u and d and
              lu and not ld):
            return 39
        elif (not l and r and u and d and
              not rd and ru):
            return 40
        elif (l and r and u and d and
              not lu and ld and not rd and ru):
            return 41
        elif (l and r and u and d and
              lu and not ld and rd and not ru):
            return 42
        elif (l and r and u and d and
              not lu and not ld and rd and not ru):
            return 43
        elif (l and r and u and d and
              not lu and ld and not rd and not ru):
            return 44
        elif (l and r and u and d and
              not lu and not ld and not rd and ru):
            return 45
        elif (l and r and u and d and
              lu and not ld and not rd and not ru):
            return 46
        else: return 0
        
    def get_new_instance(self, level, il):
        return Ground(level, il, 0, 0, 0)
    def bullet_hit(self, pos):
        pass
    def event_death(self):
        level = self.level
        self.rect = (self.rect[0] - 4, self.rect[1] - 4,
                     self.rect[2] + 8, self.rect[3] + 8)
        
        b_lu = (self.rect[0], self.rect[1], 2, 2)
        b_l = (self.rect[0], self.rect[1] + self.rect[3]/2 - 1, 2, 2)
        b_ld = (self.rect[0], self.rect[1] + self.rect[3] - 2, 2, 2)
        b_d = (self.rect[0] + self.rect[2]/2 - 1, self.rect[1] + self.rect[3] - 2, 2, 2)
        b_rd = (self.rect[0] + self.rect[2] - 2, self.rect[1] + self.rect[3] - 2, 2, 2)
        b_r = (self.rect[0] + self.rect[2] - 2, self.rect[1] + self.rect[3]/2 - 1, 2, 2)
        b_ru = (self.rect[0] + self.rect[2] - 2, self.rect[1], 2, 2)
        b_u = (self.rect[0] + self.rect[2]/2 - 1, self.rect[1], 2, 2)
        
        objs = []
        objs.append(self.find_coll_obj(b_lu, level))
        objs.append(self.find_coll_obj(b_l, level))
        objs.append(self.find_coll_obj(b_ld, level))
        objs.append(self.find_coll_obj(b_d, level))
        objs.append(self.find_coll_obj(b_rd, level))
        objs.append(self.find_coll_obj(b_r, level))
        objs.append(self.find_coll_obj(b_ru, level))
        objs.append(self.find_coll_obj(b_u, level))
        
        for o in objs:
            if o != None:
                o.set_image_index(o.find_image_index(level), level.il)
            
